<?php
require '../../cn-config.php';
$cn_interpret_url = FALSE;
require CN_DOCUMENT_ROOT.'includes/load.php';
require CN_DOCUMENT_ROOT.'modules/firstdata-ecommerce/lphp.php';


global $cndb;
$lphp = new lphp;

/** Handles transactions by sending to the firstdata servers */

$order = array();


// From Constants

$order['host'] = FIRSTDATA_HOST;
$order['port'] = FIRSTDATA_PORT;
$order['keyfile'] = FIRSTDATA_KEYFILE;

// From user configuration

$order['configfile'] = cn_get_option('firstdata_storenum');
$order['cardnumber'] = $_REQUEST['firstdata_cardnumber'];
$order['cardexpmonth'] = $_REQUEST['firstdata_cardexpmonth'];
$order['cardexpyear'] = $_REQUEST['firstdata_cardexpyear'];
$order['ordertype'] = $_REQUEST['firstdata_ordertype'];
$order['chargetotal'] = $_REQUEST['firstdata_chargetotal'];
$order['debugging'] = TRUE;
$order['verbose'] = TRUE;

// Get the price from the product id

$product = $_REQUEST['firstdata_product_id'];
$price = $cndb->get_var("SELECT `fdp_price` FROM `firstdata_products` WHERE `fdp_id`='$product'");

$count = $_REQUEST['firstdata_product_count'];

$order['chargetotal'] = $price * $count;

// Calculate tax if applicable

// Send information securely to first data
$result = $lphp->curl_process($order);  # use curl methods

	if ($result["r_approved"] != "APPROVED")    // transaction failed, print the reason
	{
		print "Status:  $result[r_approved]<br>\n";
		print "Error:  $result[r_error]<br><br>\n";
	}
	else	// success
	{		
		print "Status: $result[r_approved]<br>\n";
		print "Transaction Code: $result[r_code]<br><br>\n";
	}

var_dump($result);
# if verbose output has been checked,
# print complete server response to a table
	if ($order['verbose'])
	{
		echo "<table border=1>";

		while (list($key, $value) = each($result))
		{
			# print the returned hash 
			echo "<tr>";
			echo "<td>" . htmlspecialchars($key) . "</td>";
			echo "<td><b>" . htmlspecialchars($value) . "</b></td>";
			echo "</tr>";
		}	
			
		echo "</TABLE><br>\n";
	}







?>